package com.qk.management.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper RoleMapper;
    @Override
    public PageResult<Role> page(String name, Integer label, Integer page, Integer pageSize) {
        Integer total = RoleMapper.count(name,label);
        Integer offset = (page-1)*pageSize;
        List<Role> rows = RoleMapper.selectPage(name,label,offset,pageSize);
        return PageResult.<Role>builder()
                .total(total)
                .rows(rows)
                .build();
    }

    @Override
    public void add(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        RoleMapper.insert(role);
    }

    @Override
    public Role getById(Integer id) {
        Role role =RoleMapper.selectById(id);
        return role;
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        RoleMapper.update(role);
    }

    @Override
    public void deleteById(Integer id) {
        RoleMapper.deleteById(id);
    }

    @Override
    public List<Role> getAllRoles() {
        return RoleMapper.getAllRoles();
    }
}
